package com.artron.mediaartron.ui.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;

/**
 * Class description here
 *
 * @author doing
 * @version 1.0.0
 * @since 2017-09-28.
 */

public class TransformCustomImageView extends TransformImageView {

    private static final String TAG = "TransformCustomImageVie";

    private OnScaleChangeListener mOnScaleChangeListener;
    float[] mMatrixValues = new float[9];

    public TransformCustomImageView(Context context) {
        this(context, null);
    }

    public TransformCustomImageView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TransformCustomImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void setImageDrawable(Drawable drawable) {
        super.setImageDrawable(drawable);
        if (drawable != null && mImageRect != null) {
            refreshImageRect();
        }
    }

    @Override
    public void setImageBitmap(Bitmap bm) {
        if (bm != null) {
            Log.d(TAG, "setImageBitmap: height：" + bm.getHeight()
                    + "\twidth：" + bm.getWidth());
        }
        super.setImageBitmap(bm);
    }

    public void recovery() {
        this.isReFix = false;
        this.requestLayout();
    }

    public RectF getImageRectF() {
        return mImageRect;
    }

    public Matrix getEditImageMatrix() {
        return mMatrix;
    }

    @Override
    protected float scale(MotionEvent event) {
        float scaleFactor = super.scale(event);
        if (mOnScaleChangeListener != null) {
            mOnScaleChangeListener.onScale(this, scaleFactor);
        }
        return scaleFactor;
    }

    public void setOnScaleChangeListener(OnScaleChangeListener onScaleChangeListener) {
        mOnScaleChangeListener = onScaleChangeListener;
    }

    public void setScaleFactor(float scaleFactor) {
        mScaleFactor = scaleFactor;
    }

    public interface OnScaleChangeListener {

        void onScale(TransformCustomImageView iv, float scaleFactor);
    }

    public float getScaleFactor() {
        return mScaleFactor;
    }

    @Override
    protected void initImgPositionAndSize() {
        super.initImgPositionAndSize();
        if (mOnScaleChangeListener != null) {
            mOnScaleChangeListener.onScale(this, getScaleFactor());
        }
    }
}
